package mamikeji.liaowa.ui.activity.right.update;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.Wa_BabyMSG_Bean;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;
import mamikeji.liaowa.utils.MaxTextLength;

public class UpdateWaNameActivity extends BaseActivity {
    private static final String TAG = "UpdateWaNameActivity";
    private EditText editText_update_waname;

    String babyID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_wa_name);
        setTitleLeftBtn();
        getBase();
        setTitleName(R.string.user_msg_update_name);
        setTitleRightBtn(R.string.user_msg_update_save, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String nameUndisposed = editText_update_waname.getText().toString();
                String regex = "\\s+";
                //表示一个或多个空格的正则表达式
                nameUndisposed = nameUndisposed.trim();
                //去掉字符串开头和结尾的空格
                String nameDispose = nameUndisposed.replaceAll(regex, "");
                //去掉所有的空格
                if (TextUtils.isEmpty(editText_update_waname.getText().toString())) {
                    editText_update_waname.setText("");
                    ForToast.showToast(R.string.user_msg_update_error);
                    return;
                }
                getUpdateName(nameDispose);
            }
        });

        Intent intent = getIntent();
        babyID = intent.getStringExtra(ForContents.WA_ID);
        editText_update_waname = (EditText) findViewById(R.id.editText_update_waname);

        editText_update_waname.setText(intent.getStringExtra(ForContents.WA_nickname));

        InputFilter[] filters = new InputFilter[]{new MaxTextLength(16)};
        editText_update_waname.setFilters(filters);
    }

    private void getUpdateName(String s) {
        showLoading();
        Map<String, String> formMap = new HashMap<>();
        formMap.put(HttpKey.UPDATE_WA_USERCODE, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.UPDATE_WA_id, babyID);
        formMap.put(HttpKey.UPDATE_WA_nickName, s);
        formMap.put(HttpKey.UPDATE_WA_sex, "");
        formMap.put(HttpKey.UPDATE_WA_weight, "");
        formMap.put(HttpKey.UPDATE_WA_height, "");
        formMap.put(HttpKey.UPDATE_WA_birthtime, "");
        formMap.put(HttpKey.UPDATE_WA_constellation, "");
        formMap.put(HttpKey.UPDATE_WA_describe, "");
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.UPDATE_USERBADY_MSG, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
                hideLoading();
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        Wa_BabyMSG_Bean wa_babyMSG_bean = new Gson().fromJson(response, Wa_BabyMSG_Bean.class);
                        ForPreference.putString(ForContents.WA_nickname, wa_babyMSG_bean.getUserBaby().getNickName());
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                        finish();
                    }
                    ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    hideLoading();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
